﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChinaDynastyTours.Controls;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;

namespace ChinaDynastyTours.Views
{
    public  enum PopupContainerKind { GirdViewType, StandType }
    public partial class SelectTool : UserControl
    {
        private PopupContainerKind _PopContainerKind;
        private PopupContainerControl _OwnerPopupContainerControl;
        private PopupContainerEdit _OwnerPopupContainerEdit;
        private RepositoryItemPopupContainerEdit _OwnerRepositoryItemPopupContainerEdit;
        private DataTable _DataSource;

        [Description("选择内容后返回的城市名称")]
        public string CityName { get; set; }

        [Description("所属的容器控件的类型-GirdViewType为RepositoryItemPopupContainerEdit，StandType为PopupContainerControl")]
        public PopupContainerKind PopupContainerKind
        {
            get { return _PopContainerKind; }
            set { _PopContainerKind = value; }
        }

        [Description("所属的弹出容器控件-PopupContainerControl")]
        public PopupContainerControl OwnerPopupContainerControl
        {
            get { return _OwnerPopupContainerControl; }
            set { 
                    _OwnerPopupContainerControl = value;
                    _OwnerPopupContainerControl.Controls.Add(this);
            } 
        }

        [Description("所属的弹出容器编辑器-PopupContainerEdit")]
        public PopupContainerEdit OwnerPopupContainerEdit
        { 
            get { return _OwnerPopupContainerEdit; }
            set
            {
                if (_PopContainerKind == PopupContainerKind.StandType)
                {
                    _OwnerPopupContainerEdit = value;
                    OwnerPopupContainerEdit.Properties.PopupControl = OwnerPopupContainerControl;
                }
            }
        }

        [Description("所属的弹出容器编辑器-PopupContainerEdit")]
        public RepositoryItemPopupContainerEdit OwnerRepositoryItemPopupContainerEdit 
        {
            get { return _OwnerRepositoryItemPopupContainerEdit; }
            set
            {
                if (_PopContainerKind == PopupContainerKind.GirdViewType)
                {
                    _OwnerRepositoryItemPopupContainerEdit = value;
                    OwnerRepositoryItemPopupContainerEdit.PopupControl = OwnerPopupContainerControl;
                }
            }
        }

        [Description("数据源")]
        public DataTable DataSource 
        {
            get { return _DataSource; }
            set { 
                _DataSource = value;
                cityEdit.Properties.DataSource = _DataSource; //BaseInfoCtl.GetCityData();
            } 
        }

        public SelectTool()
        {
            InitializeComponent();
        }

        private void tbCityEdit_DoubleClick(object sender, EventArgs e)
        {
            tbCityEdit_KeyPress(tbCityEdit,new KeyPressEventArgs(Char.Parse("\r")));
        }

        private void tbCityEdit_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Char.Parse("\r"))
            {
                CityName = tbCityEdit.Text;
                OwnerPopupContainerControl.OwnerEdit.ClosePopup();
            }
        }

        private void cityEdit_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            CityName = cityEdit.EditValue == null ? "" : cityEdit.EditValue.ToString();
            OwnerPopupContainerControl.OwnerEdit.ClosePopup();
        }

        private void cityEdit_TextChanged(object sender, EventArgs e)
        {
            tbCityEdit.EditValue = cityEdit.EditValue;
        }

        //文本输入框按钮单击处理
        private void tbCityEdit_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.OK)
            {
                CityName = tbCityEdit.Text;
                OwnerPopupContainerControl.OwnerEdit.ClosePopup();
            }
        }
    }
}
